package com.lib.security;

import android.content.Context;
import android.util.Log;
import com.zlm.zy.zhoujuanjuan.a.AESEncrypt;
import com.zlm.zy.zhoujuanjuan.a.CommentUtil;
import com.zlm.zy.zhoujuanjuan.a.EncodeUtils;
import com.zlm.zy.zhoujuanjuan.a.RSAEncrypt;
import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 创建时间: 2018/09/06 19:58
 * 作者: zhoujuanjuan
 * 描述: 签名校验 + 反调试
 * Java_com_lib_security_VsckManager_loadVSFromC
 */
public class VsckManager {

  //static {
  //  System.loadLibrary("vsck");
  //}

  //因为签名使用的是V2sign会校验zip的完整性 写入comment数据会破坏导致7.0以上手机校验失败无法安装
  //public static String getCommentStr(Object context) {
  //  return CommentUtil.getZipComment(CommentUtil.getPackagePath((Context) context));
  //}

  public static String getRandomString(int length) {
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
      int number = random.nextInt(3);
      long result;
      switch (number) {
        case 0:
          result = Math.round(Math.random() * 25 + 65);
          sb.append(String.valueOf((char) result));
          break;
        case 1:
          result = Math.round(Math.random() * 25 + 97);
          sb.append(String.valueOf((char) result));
          break;
        case 2:
          sb.append(String.valueOf(new Random().nextInt(10)));
          break;
      }
    }
    Log.d("zjj", "-----RandomStr-----" + sb.toString());
    return sb.toString();
  }

  public static String getRStr() {
    String str = getRandomString(6);
    byte[] bs = str.getBytes();
    try {
      String utfStr = new String(bs, "UTF-8");//一定要转化成UTF-8
      return base64Enc(utfStr);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static byte[] base64Dec(String str) {
    return EncodeUtils.base64Decode(str);
  }

  public static String base64Enc(String str) {
    return EncodeUtils.base64Encode(str.getBytes());
  }

  public static String getFStr(String publicKey, byte[] str) {
    try {
      byte[] data = RSAEncrypt.decrypt(RSAEncrypt.getPublicKey(publicKey), str);
      String finalStr = new String(data);
      Log.e("jni-test", "-----finalStr----" + finalStr);
      return finalStr;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  // so里调用这个
  public static String p2local(String p, String k) {
    return AESEncrypt.encryptAESToBase64(p, k);
  }

  public static String local2p(String src, String k) {
    return AESEncrypt.decryptBase64ToAES(src, k);
  }

  /**
   * @param channel 4：案场 5：房江湖  如有增加或修改同步so代码修改
   */
  public static native boolean loadVSFromC(Object context, int channel);

  public static native String encP2local(String p);

  public static native String decLocal2P(String src);

  public static native String encLP2Net(String src);
}
